package com.ch04.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ch04.pojo.BackendUser;
import com.ch04.service.BackendUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author Jane
 * @date 2024-05-27 16:29
 */

@Controller
@RequestMapping("/backenduser")
public class BackendUserController {

    @Resource
    BackendUserService backendUserService;

    @RequestMapping("/dologin")
    public String dologin(String userCode, String userPassword, HttpSession session, Model model){
        QueryWrapper<BackendUser> queryWrapper=new QueryWrapper();
        queryWrapper.eq("usercode",userCode);
        queryWrapper.eq("userpassword",userPassword);
        BackendUser backendUser=backendUserService.getOne(queryWrapper);
        if(backendUser != null){
            session.setAttribute("backendUser",backendUser);
            return "redirect:/backenduser/tomain";
        }else {
            return "redirect:/backend/login";
        }
    }
    @RequestMapping("/tomain")
    public String tomain(){
        System.out.println("---------------");
        return "main";
    }

    @RequestMapping("/clear")
    public String clear(HttpSession session){
        if(session.getAttribute("backendUser")!=null){
            session.removeAttribute("backendUser");
        }
        return "redirect:/backend/login";
    }
   @RequestMapping("/add")
    public int addBackendUser(String userName,String userPassword
            ,String userType,String userCode){
        BackendUser backendUser=new BackendUser();
        backendUser.setUsername(userName);
        backendUser.setUserpassword(userPassword);
        backendUser.setUsertype(Long.valueOf(userType));
        backendUser.setUsercode(userCode);
        return backendUserService.save(backendUser)?1:-1;
    }
}
